﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using StructureMap;

namespace MedicalCare.DbLayer.Adaptors
{
    public class StandardValueAdaptor : IStandardValueAdaptor
    {
        private ISession _mcSession;

        public StandardValueAdaptor()
        {
            _mcSession = ObjectFactory.GetNamedInstance<ISession>("MedicalCareSession");
        }

        public IList<StandardValueEntity> GetAllStandardValue(StandardValueMessage message)
        {
            try
            {
                var list = _mcSession.CreateCriteria<StandardValueEntity>()
                    .Add(Restrictions.Eq("EquipmentTypeId", message.EquipmentTypeId))
                    .List<StandardValueEntity>();

                return list;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return null;
        }


        public IList<StandardValueEntity> GetPagingRecords(string orderKey, string orderVal,
                                 int pageIndex, int pageSize, string whereStr)
        {
            throw new NotImplementedException();
        }

        public int GetRecordsTotal(string whereStr)
        {
            throw new NotImplementedException();
        }

        public int Update(StandardValueMessage msg)
        {
                if(msg.Id>0)
                {
                    //TODO:Update
                }
                else
                {
                    var query = _mcSession.CreateSQLQuery(@" EXEC StandardValue_Insert 
                                                            @Id=:Id,
                                                            @EquipmentTypeId=:EquipmentTypeId,
                                                            @ItemName=:ItemName,
                                                            @Value=:Value,
                                                            @Unit=:Unit,
                                                            @CreateUser=:CreateUser")
                                 .SetInt32("Id", msg.Id) //output
                                .SetInt32("EquipmentTypeId", msg.EquipmentTypeId)
                                .SetString("ItemName", msg.ItemName)
                                .SetDecimal("Value", Convert.ToDecimal(msg.Value))
                                .SetString("Unit", msg.Unit)
                                .SetString("CreateUser", msg.CreateUser)
                               .SetResultTransformer(Transformers.AliasToBean<ResultEntity>());
                    var result = query.List<ResultEntity>();
                    return result.FirstOrDefault().Id;
                }

            throw new NotImplementedException();
        }

        public void Remove(StandardValueMessage opp)
        {
            throw new NotImplementedException();
        }

        public StandardValueMessage Read(string id)
        {
            throw new NotImplementedException();
        } 
    }
}
